package com.demo1.servlet; /**
 * @author wzm
 * @create 2023-11-07 11:34
 * project_name:web2310
 * @Description
 */

import com.demo1.dao.BaseDao;
import com.demo1.dao.UserDao;
import com.demo1.entity.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //0.设置编码方式
        request.setCharacterEncoding("UTF-8");//请求
        response.setContentType("text/html;charset=UTF-8");//响应

        //1.接收前端参数用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2.查询数据库
//        UserDao userDao = new UserDao();
//        User user = userDao.selectByNameAndPassWord(new User(null, username, password));
        BaseDao baseDao = new BaseDao();
        String sql = "select * from tb_user where username = ? and password = ?";
        User user = baseDao.selectbyOne(User.class, sql, username, password);


        //3.根据结果进行判断

        PrintWriter writer = response.getWriter();
        if (user != null) {
            //登录成功
            //4.响应结果到浏览器
          writer.write("登录成功");
        }else{
            //登录失败
           writer.write("登录失败");
        }
        


    }
}
